package com.alibaba.service;

import com.alibaba.common.ResponseResult;
import com.alibaba.define.domain.LinkDO;
import com.alibaba.define.domain.vo.LinkVO;
import com.alibaba.define.service.LinkService;
import com.alibaba.manager.LinkManager;
import com.alibaba.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.define.enums.AppHttpCodeEnum.SYSTEM_EXECUTE_ERROR;

/**
 * @author : hanyang
 * @date : 2023-11-02 23:29
 **/

@Slf4j
@Service
public class LinkServiceImpl implements LinkService {

    @Resource
    LinkManager linkManager;

    @Override
    public ResponseResult<List<LinkVO>> getAllLink() {
        List<LinkVO> linkVOS = new ArrayList<>();
        try {
            List<LinkDO> allPassLink = linkManager.getAllPassLink();
            if (CollectionUtils.isEmpty(allPassLink)) {
                return ResponseResult.ok(linkVOS);
            }
            // DO => VO
            linkVOS = BeanCopyUtils.copyBeanList(allPassLink, LinkVO.class);
            return ResponseResult.ok(linkVOS);
        } catch (Exception e) {
            log.error("[LinkService] getAllLink list error!", e);
        }
        return ResponseResult.error(SYSTEM_EXECUTE_ERROR.getCode(), SYSTEM_EXECUTE_ERROR.getMsg());
    }
}
